<template>
    <div  class="p16 flex-column-null-between container">
       <el-row>
            <el-col :span="6">
                <div class="grid-content bg-purple" @click="OptionCardClose">
                    <div slot="header" class="top-from">组织列表
                        <el-button style="margin-left: 50px" type="success" size="mini" icon="el-icon-refresh"  @click="refreshDept">刷新</el-button>
                    </div>
                    <div style="margin-top:2px;overflow: auto;height: calc(100vh - 250px)">
                        <el-tree
                            :data="deptData"
                            :props="defaultProps"
                            default-expand-all
                            highlight-current
                            :expand-on-click-node="deptExpandOnClickNode"
                            @node-click="handleNodeClick"
                            @node-contextmenu="contextMenu">

                        </el-tree>
                    </div>
                    <div class="right-menu" :style="{'z-index':999,'position':'fixed',left:leftNumber + 'px',
                                top: topNumber + 'px',
                                width:'80px',
                                background:'white','box-shadow':'0 2px 4px rgba(0,0,0,.12),0 0 6px rgba(0,0,0,.04)'}" 
                                v-show="optionCardShow"
                                id="option-button-group">
                        <el-button @click="deptSelect" class="option-card-button">查看</el-button>
                        <el-button @click="deptAdd" class="option-card-button">新增</el-button>
                    </div>
                </div>
                <!-- 添加组织 -->
                <addDept ref="addDept" @success="addNewDeptTo"/>
                <!-- 更新组织 -->
                <editDept ref="editDept" @success=""/>
            </el-col>

            <el-col :span="9">
                <div class="grid-content bg-purple-light">
                    <div slot="header" class="top-from">组织关联岗位： {{nowDept.deptName}}
                        <el-button v-if="nowDept.deptName != null" style="margin-left: 20px" type="primary" size="mini"  @click="addPositionVo">添加</el-button>
                        <el-button v-if="nowDept.deptName != null" style="margin-left: 20px" type="primary" size="mini"  @click="deptAllUser">所有员工</el-button>
                    </div>
                   <div style="overflow: auto;height: calc(100vh - 250px)">
                        <el-table
                            :data="positionTable"
                            size="small"
                            @current-change="handlePositionChange"
                            :row-style="{height:'0'}"
                            style="width: 100%"
                            :highlight-current-row="positionHighLight"
                            v-loading="positionLoading">
                            <el-table-column
                                prop="positionCode"
                                label="岗位代码"
                                min-width="50">
                            </el-table-column>
                            <el-table-column
                                prop="positionName"
                                label="岗位名称"
                                min-width="40">
                            </el-table-column>
                            <el-table-column
                                prop="positionLevel"
                                min-width="20"
                                label="等级">
                            </el-table-column>
                              <el-table-column
                                prop="operate"
                                min-width="30"
                                label="操作">
                                <template  scope="scope">
                                    <el-button @click="removePosition(scope.row)" type="text" size="mini">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>

                   </div>
                </div>
                
                <!-- 添加岗位 -->
                <addPosition ref="addPosition" @success="queryDeptPosition()"/>
            </el-col>

            <el-col :span="9">
                <div class="grid-content bg-purple">
                    <div slot="header" class="top-from">岗位关联员工： {{nowPosition.positionName}}
                        <el-button v-if="nowPosition.positionName != null" style="margin-left: 20px" type="primary" size="mini"  @click="addPositionUser">添加</el-button>
                    </div>

                    <div style="overflow: auto;height: calc(100vh - 250px)">
                        <el-table
                            :data="userTable"
                            size="small"

                            :row-style="{height:'0'}"
                            style="width: 100%"
                            v-loading="positionUserLoading">
                            <el-table-column
                                prop="userAccount"
                                label="用户账号"
                                min-width="50">
                            </el-table-column>
                            <el-table-column
                                prop="username"
                                label="用户名称"
                                min-width="50">
                            </el-table-column>
                            <el-table-column
                                prop="operate"
                                min-width="30"
                                label="操作">
                                <template  scope="scope">
                                    <el-button @click="removePositionUser(scope.row)" type="text" size="mini">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </div>
                </div>
                
                <!-- 添加岗位员工 -->
                <addUser ref="addUser" @success="queryPositionUsers()"/>
            </el-col>
       </el-row>
    </div>
</template>

<script>
    import addDept  from "./menu/AddDept";
    import editDept  from "./menu/EditDept";
    import addPosition  from "./menu/AddPosition";
    import addUser  from "./menu/AddUser";
    import{queryDeptVo,queryOne,queryDeptPositions,removeDeptPosition,queryPositionUsers,queryDeptUsers,removeDeptPositionUser} from '@/api/dept';
    export default {
        components:{
            addDept,
            editDept,
            addPosition,
            addUser
        },
        data() {
            return {
                positionHighLight:true,
                deptExpandOnClickNode:false,
                positionLoading:false,
                positionUserLoading:false,
                topNumber:0,
                leftNumber:0,
                optionCardShow:false,
                //部门树
                deptData:[],
                defaultProps:{
                    children: 'child',
                    label: 'deptName'
                },
                //当前选中部门
                nowDept:{},
                //当前右键操作部门
                nowEditDept:{},
                //当前选中岗位
                nowPosition:{},
                //岗位table
                positionTable:[],
                //岗位关联员工table
                userTable:[]
            }

        },
        created(){
             this.queryDeptData();
        },
        methods:{
            //刷新
            refreshDept(){
                this.queryDeptData();
                this.nowEditDept = {};
                this.nowDept = {};
                this.nowPosition = {};
                this.positionTable = [];
                this.userTable = [];
            },
            async queryDeptData(){
                let res = await queryDeptVo();
                let data = res.data;
                this.deptData = data.child;
            },
            addNewDeptTo(dept,child){
              if(dept.child == null){
                dept.child = [];
              }
              dept.child.push(child);
            },
            handleNodeClick(dept){
                if(dept == null){
                    return;
                }
                this.nowDept = dept;
                this.nowPosition = {};
                this.positionTable = [];
                this.userTable = [];
                this.queryDeptPosition();
            },
            // 点击框外区域 隐藏菜单
            OptionCardClose(event) {
                var currentCli = document.getElementById("option-button-group");
                if (currentCli) {
                    if (!currentCli.contains(event.target)) { 
                        //点击到了id为option-button-group以外的区域，就隐藏菜单
                        this.optionCardShow = false;
                    }
                }
            },
            //鼠标右键部门
            contextMenu(event,data){
                this.optionCardShow = false
                this.nowEditDept = data;

                let screenHeight = window.innerHeight;
                let nowY = event.pageY + 20;
                if(nowY > screenHeight - 80){
                  this.topNumber = nowY - 100;
                }else{
                  this.topNumber = event.pageY + 20;
                }
                this.leftNumber = event.pageX;
                console.log(this.topNumber);
                console.log(this.leftNumber);
                this.optionCardShow = true;
            },
            async deptSelect(){
                let deptId = this.nowEditDept.deptId;
                let res = await queryOne({deptId:deptId});
                this.$refs["editDept"].showDialog(res.data);
                this.optionCardShow = false;
            },
            deptAdd(){
                this.$refs["addDept"].showDialog(this.nowEditDept);
                this.optionCardShow = false;
            },
            //查询部门岗位
            async queryDeptPosition(){
                this.positionLoading = true;
                let res = await queryDeptPositions({deptCode:this.nowDept.deptCode});
                this.positionTable = res.data;
                this.positionLoading = false;
            },
            //删除岗位
            async removePosition(row){
                this.$confirm('您是否要删除该部门岗位?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    await removeDeptPosition({relateId:row.relateId});
                    this.queryDeptPosition();
                }).catch(() => {
                });
            },
            handlePositionChange(row){
                if(row == null){
                    return;
                }
                this.nowPosition = row;
                this.queryPositionUsers();
            },
            async deptAllUser(){
                this.positionUserLoading = true;
                //查询部门下所有人员
                let res = await queryDeptUsers({deptCode:this.nowDept.deptCode});
                this.userTable = res.data;
                this.positionUserLoading = false;
            },
            //查询岗位下员工
            async queryPositionUsers(){
                if(this.nowPosition == null){
                    return;
                }
                this.positionUserLoading = true;
                let res = await queryPositionUsers({deptCode:this.nowDept.deptCode,
                         positionCode:this.nowPosition.positionCode});
                this.userTable = res.data;
                this.positionUserLoading = false;
            },
            removePositionUser(row){
                this.$confirm('您是否要删除该岗位员工?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    await removeDeptPositionUser({relateId:row.relateId});
                    this.queryPositionUsers();
                }).catch(() => {
                });
            },
            addPositionVo(){
                this.$refs["addPosition"].showDialog(this.nowDept.deptCode);
            },
            addPositionUser(){
                this.$refs["addUser"].showDialog(this.nowDept.deptCode,this.nowPosition.positionCode);
            }
        }

    }
</script>


<style src="../css/index.scss" lang="scss"></style>
<style lang="scss" scoped>
   
    .folder-box {
        height: 100%;
    }

    .option-card-button {
        width: 100%;
        margin-left: 0;
        font-size: 12px;
        border-radius: 0;
    }
    .search-item{
        float: left;
        margin: 0 20px 20px 0;
        span{
            color: #606266;
            font-size: 14px;
            margin-right: 10px;
        }
    }

 .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    /* background: #d3dce6; */
  }
  .bg-purple-light {
    /* background: #e5e9f2; */
  }
  .top-from{
      height: 50px;
  }
  .grid-content {
    border: 1px solid #e1e4e7;
    border-radius: 4px;
    margin-top: 0px;
    background: #fff;
    //设置内容高度随浏览器变化
    height: calc(100vh - 160px);
    //min-height:  600px;
    overflow: auto;
    padding-top: 15px;
    padding-left: 20px;
    padding-right: 20px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }

</style>
